package com.qf.activiti.demo;

import org.activiti.engine.*;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import sun.nio.ch.IOUtil;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivitiAdvance {

    /**
     * 流程定义的查询
     */
    @Test
    public void test() {
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取RepositoryService(资源)
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //3.查询流程定义
        // select * from act_re_prodef
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        for (ProcessDefinition processDefinition : list) {
            System.out.println("流程定义的名称：" + processDefinition.getName());
            System.out.println("流程定义的key:" + processDefinition.getKey());
        }
    }


    @Test
    public void test1() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        taskService.complete("2508");
    }

    /**
     * 资源文件的下载
     */
    @Test
    public void test2() throws IOException {
        //1.获取资流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取资源service
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        /**
         * deploymentId: 部署对象id
         * resourceName: 资源名称
         */
        //查询到流程定义对象，就可以拿到参数repositoryService.getProcessDefinition
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
        //拿到bpmn
        InputStream bpmnInputStream = repositoryService
                .getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
        //拿到图片
        InputStream pngInputStream = repositoryService
                .getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getDiagramResourceName());

        FileOutputStream leaveOutputStream = new FileOutputStream("E:\\out\\leave.pbmn");
        FileOutputStream pngOutputStream = new FileOutputStream("E:\\out\\leave.png");

        //以输出流的形式输出到磁盘
        IOUtils.copy(bpmnInputStream, leaveOutputStream);
        IOUtils.copy(pngInputStream, pngOutputStream);

        leaveOutputStream.close();
        pngOutputStream.close();
    }


    /**
     * 流程定义的删除
     */
    @Test
    public void test3() {
        //1获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2获取资源对象
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //流程定义查询
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
        //3删除流程定义  先删除Deployment ，然后删除流程定义表
        /**
         * 第一个参数：部署对象的id
         * 第二个参数：级联删除
         */
//        repositoryService.deleteDeployment(processDefinition.getDeploymentId());
        repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
    }

    /**
     * 获取businessKey
     */
    @Test
    public void test4() {
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.TaskService
        TaskService taskService = defaultProcessEngine.getTaskService();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        List<Task> list = taskService.createTaskQuery().list();
        for (Task task : list) {
            //处理任务
            //先查询审批信息
            //拿到流程实例id
            String processInstanceId = task.getProcessInstanceId();
            //根据流程实例id 拿到流程实例
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
            //通过流程实例拿到businessKey  ===业务表的id
            String businessKey = processInstance.getBusinessKey();
            //上业务表查询数据
            System.out.println(businessKey);
        }
    }


    /**
     * 流程定义的挂起和激活
     */
    @Test
    public void test5() {
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取资源的service
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        //3.查询流程
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().singleResult();
//        System.out.println("当前的挂起状态为：" + repositoryService.isProcessDefinitionSuspended(processDefinition.getId()));
        boolean suspended = repositoryService.isProcessDefinitionSuspended(processDefinition.getId());
        if (suspended) {
            System.out.println("已激活");
//            repositoryService.activateProcessDefinitionById(processDefinition.getId());
            repositoryService.activateProcessDefinitionById(processDefinition.getId(), true, null);

        } else {
            System.out.println("已挂起");
//            repositoryService.suspendProcessDefinitionById(processDefinition.getId());
            /**
             * 第一个参数：流程定义id
             * 第二个参数：是否要将流程定义下的流程实例也挂起
             * 第三个参数：挂起的日期  null表立即挂起
             */
            repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);
        }
    }


    /**
     * 流程实例的挂起和激活
     */
    @Test
    public void test6() {
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取运行的RunService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //根据流程实例id获取到流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
                .processInstanceId("17501")
                .singleResult();
        //判断当前流程实例是否挂起
        if (processInstance.isSuspended()) {
            System.out.println("已激活");
            runtimeService.activateProcessInstanceById(processInstance.getId());
        } else {
            System.out.println("已挂起");
            runtimeService.suspendProcessInstanceById(processInstance.getId());
        }
    }

    /**
     * 启动流程实例
     * UEL表达式去分配负责人
     */
    @Test
    public void test7(){
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取运行的RunService
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        Map<String,Object> map =new HashMap();
        map.put("S1","张三");
        map.put("S2","李四");
        runtimeService.startProcessInstanceByKey("leaveUEL",map);
    }


    /**
     * 查询候选人处理任务
     */
    @Test
    public void test8(){
        //1.获取流程引擎对象
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取任务Service
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.获取
        List<Task> list = taskService.createTaskQuery().taskCandidateUser("李四").list();
        for (Task task : list) {
            System.out.println("任务名称:"+task.getName());
        }
    }

    /**
     * 任务的领取,让候选人成为负责热任
     */

    @Test
    public void test9(){
        //1.获取流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        //2.获取任务的service
        TaskService taskService = defaultProcessEngine.getTaskService();
        //3.领取任务
        taskService.claim("2506","张三");
    }
}
